#include "clientHead.h"
typedef struct{
    int length;
    char data[1000];
}train_t;
int dealCommand(int sockfd, char *username){
    static char cHead[4096] = {0};
    // 初始化聊天头
    int len = strlen(username);
    for(int i = 0; i < len; i++){
        cHead[i] = username[i];
    }
    printf("> %s: ", cHead);
    fflush(stdout);

    char buffer[4096] = {0};
    char command[4096] = {0};
    char parameter[4096] = {0};
    char strtokBuffer[4096] = {0};
    // 获取stdin命令和参数
    fgets(buffer, sizeof(buffer), stdin);
    // 去除最后一个换行符
    if(buffer[strlen(buffer)-1] == '\n'){
        buffer[strlen(buffer)-1] = 0;
    }
    // 复制客户端输入
    memcpy(strtokBuffer, buffer, sizeof(buffer));
    const char delimiters[] = " "; // 截断依据
    // 截取命令
    char *token = strtok(strtokBuffer, delimiters);
    memcpy(command, token, strlen(token));
    // 截取参数
    token = strtok(NULL, delimiters);
    if(token != NULL){
    memcpy(parameter, token, strlen(token));
    }
    // 小火车发送路径(聊天头)
    train_t train;
    train.length = strlen(cHead); // 设置车头
    memcpy(train.data, cHead, train.length); // 设置车厢
    send(sockfd, &train, sizeof(train.length)+train.length, MSG_NOSIGNAL);
    // 小火车发送命令+参数
    train.length = strlen(buffer);
    memcpy(train.data, buffer, train.length);
    send(sockfd, &train, sizeof(train.length)+train.length, MSG_NOSIGNAL);

    char route[4096] = {0}; // 接收路径
    char content[4096] = {0}; // 接收内容
    char mark[4096] = {0}; // 接受标志
    LOG(username, command); // 打印日志
    if(strcmp(command, "cd") == 0){
        // 如果cd后没有参数
        if(strlen(parameter) == 0){
            printf("Invalid command: cd!\n");
            return 1;
        }
        // 接收路径
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(route, train.data, train.length);
        // 接收标志
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(mark, train.data, train.length);
        if(strcmp(mark, "true") == 0){
            memcpy(cHead, route, strlen(route));
            cHead[strlen(route)] = 0; // cd ..时聊天头变短，加一个结束符
        }
        else{
            printf("cd false!\n");
        }
    }
    else if(strcmp(command, "ls") == 0){
        // 接收路径
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(route, train.data, train.length);
        // 接受内容
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(content, train.data, train.length);
        // 接收标志
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(mark, train.data, train.length);
        // 判断内容是否为空
        if(strlen(content) == 0){
            return 0;
        }
        if(strcmp(mark, "true") == 0){
            printf("%s\n", content);
        }
        else{
            printf("ls false!\n");
        }

    }
    else if(strcmp(command, "pwd") == 0){
        // 接收路径
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(route, train.data, train.length);
        // 接收标志
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(mark, train.data, train.length);
        if(strcmp(mark, "true") == 0){
            printf("%s\n", route);
        }
        else{
            printf("pwd false!\n");
        }

    }
    else if(strcmp(command, "mkdir") == 0){
        // 如果mkdir后没有参数
        if(strlen(parameter) == 0){
            printf("Invalid command: mkdir!\n");
            return 1;
        }
        // 接收路径
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(route, train.data, train.length);
        // 接收标志
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(mark, train.data, train.length);
        //printf("mark = %s\n", mark); // 打印标志
        if(strcmp(mark, "false") == 0){
            printf("mkdir false!\n");
        }

    }
    else if(strcmp(command, "rmdir") == 0){
        // 如果rmdir后没有参数
        if(strlen(parameter) == 0){
            printf("Invalid command: rmdir!\n");
            return 1;
        }
        // 接收路径
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(route, train.data, train.length);
        // 接收标志
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(mark, train.data, train.length);
        if(strcmp(mark, "false") == 0){
            printf("rmdir false!\n");
        }
    }
    else if(strcmp(command, "remove") == 0){
        // 如果remove后没有参数
        if(strlen(parameter) == 0){
            printf("Invalid command: remove!\n");
            return 1;
        }
        // 接收路径
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(route, train.data, train.length);
        // 接收标志
        recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
        recv(sockfd, train.data, train.length, MSG_WAITALL);
        memcpy(mark, train.data, train.length);
        if(strcmp(mark, "false") == 0){
            printf("remove false!\n");
        }
    }
    else if(strcmp(command, "puts") == 0){
        // 如果puts后没有参数
        if(strlen(parameter) == 0){
            printf("Invalid command: puts!\n");
            return 1;
        }
        puts_client(sockfd, parameter);
    }
    else if(strcmp(command, "gets") == 0){
        // 如果gets后没有参数
        if(strlen(parameter) == 0){
            printf("Invalid command: gets!\n");
            return 1;
        }
        gets_client(sockfd, parameter);
    }
    else{
        printf("command is not existence!\n");
    }
    return 0;
}

